package com.github.niefy.modules.wx.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.niefy.common.utils.R;
import com.github.niefy.modules.sys.service.SysUserTokenService;
import com.github.niefy.modules.wx.dao.WxUserSysUserMapper;
import com.github.niefy.modules.wx.entity.WxUserSysUser;
import com.github.niefy.modules.wx.service.WxUserSysUserService;

@Service
public class WxUserSysUserServiceimpl extends ServiceImpl<WxUserSysUserMapper, WxUserSysUser> implements WxUserSysUserService {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    private SysUserTokenService sysUserTokenService;

    // 有特殊的SQL才需要这个注入，普通的CRUD等Iservice已继承
    // @Autowired
    // private WxUserSysUserMapper wxUserSysUserMapper;
    @Override
    public boolean updateOrInsert(WxUserSysUser wxUserSysUser) {
        if (StringUtils.isEmpty(wxUserSysUser.getOpenid())) return false;
        this.saveOrUpdate(wxUserSysUser);
        return true;
    }
    
    @Override
    public WxUserSysUser queryByOpenid(String openid) {
        if (StringUtils.isEmpty(openid)) return null;
        return this.getById(openid);
    }

    @Override
    public R createToken(String openid) {
        if (StringUtils.isEmpty(openid)) return R.error("openid 为空");
        WxUserSysUser wu =  this.getById(openid);
        if (wu.getStatus()!= 1) return R.error("这个用未审核，请联系人员审核");
        return sysUserTokenService.createToken(wu.getUserId());
    }

    @Override
    public void logout(long userId) {
        sysUserTokenService.logout(userId);
    }
    
}
